package com.bestv.search.engine.command;

import com.bestv.search.common.util.DateUtil;
import com.bestv.search.engine.compass.extend.HibernateGpsDeviceExtend;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compass.gps.CompassGps;

import java.util.Date;

public class IndexCommand {
    protected final Log log = LogFactory.getLog(getClass());
    private HibernateGpsDeviceExtend coreDevice;

    private CompassGps compassGps;

    public CompassGps getCompassGps() {
        return compassGps;
    }

    public void setCompassGps(CompassGps compassGps) {
        this.compassGps = compassGps;
    }

    public String index(boolean always) {
        if (!always)
            return null;
        String result;
        long time = System.currentTimeMillis();
        compassGps.index();
        time = System.currentTimeMillis() - time;
        result = "{'result' : 'success', 'indexTime' : '" + time + " ms'}";
        log.debug(result);
        return result;
    }

    public String index() {
        String result = "";
        if (!coreDevice.isCanStartIncrementIndexThread()) {
            log.info("The increasing index thread is running, waiting until the thread finished......");
            return "The thread is busy, please try later!";
        }
        if (coreDevice.isCanStartIncrementIndexThread()) {
            coreDevice.setCanStartIncrementIndexThread(!coreDevice.isCanStartIncrementIndexThread());// (1)
            if (coreDevice.isCanStartIncrementIndexThread()) {
                /**
                 * 说明在执行1的时候，恰好被其他线程修改了coreDevice.
                 * canStartIncrementIndexThread的状态为false，
                 * 由此可以断定有一个线程正在执行index或increasingIndex，此时该线程应该等待或者直接返回
                 */
                log.info("The increasing index thread is running , waiting until the thread finished......");
                return "The thread is busy, please try later!";
            }
            String indexTime = DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss", new Date());
            log.info("reindex time : " + indexTime);
            long time = System.currentTimeMillis();
            try {
                // Set last index time to null for start full index.
                coreDevice.resetLastIndexTime();

                // Start update index
                coreDevice.doIndex();
                // Do index for HD
                coreDevice.doIndexForHD();

                LuceneSearchCommandRefactor.setIndexSearcher(LuceneSearchCommandRefactor.refreshIndexCache("info"));
                LuceneSearchCommandRefactor.setKeySearcher(LuceneSearchCommandRefactor.refreshIndexCache("keysubject"));
                LuceneSearchCommandRefactor.setKeySearcher(LuceneSearchCommandRefactor.refreshIndexCache("hdinfo"));
            } catch (Exception e) {
                log.error("reindex failed @" + new Date());
                e.printStackTrace();
            } finally {
                coreDevice.setCanStartIncrementIndexThread(true);
            }
            long now = System.currentTimeMillis();
            result = "{'result' : 'success', 'message' : 'Index success,total time: " + (now - time) / 1000 + " s'}";
            log.info(result);
        }
        return result;
    }

    public HibernateGpsDeviceExtend getCoreDevice() {
        return coreDevice;
    }

    public void setCoreDevice(HibernateGpsDeviceExtend coreDevice) {
        this.coreDevice = coreDevice;
    }
}
